package com.cloudkinto.service.stock.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.utils.ExcelUtils;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.OrderInputDao;
import com.cloudkinto.dao.StockHistoryDetailDao;
import com.cloudkinto.dto.stock.snap.StockAgeEntity;
import com.cloudkinto.dto.stock.snap.sku.StockSkuSegmentPageDto;
import com.cloudkinto.dto.stock.snap.sku.StockSkuSnapPageDto;
import com.cloudkinto.entity.StockHistoryDetailDo;
import com.cloudkinto.service.stock.StockHistoryDetailService;
import com.cloudkinto.service.stock.exportVo.age.BoxStockAgeExportVo;
import com.cloudkinto.service.stock.exportVo.age.BoxStockSegAgeExportVo;
import com.cloudkinto.service.stock.exportVo.age.ProductStockAgeExportVo;
import com.cloudkinto.service.stock.exportVo.age.ProductStockSegAgeExportVo;
import com.cloudkinto.service.warehouse.WarehouseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author hua
 * @since 2024-12-03
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
@RequiredArgsConstructor
public class StockHistoryDetailServiceImpl extends ServiceImpl<StockHistoryDetailDao, StockHistoryDetailDo> implements StockHistoryDetailService {
    private final StockHistoryDetailDao dao;
    private final OrderInputDao orderInputDao;
    private final WarehouseService warehouseService;

    @Override
    public SingleResult pageInit(Long tenantId, Long companyId) {
        Map<String, Object> result = new HashMap<>();
        result.put("warehouseList", warehouseService.getWarehouseCompany(companyId, tenantId));
        return SingleResult.success(result);
    }

    @Override
    public List<StockSkuSnapPageDto> getList(Map<String, Object> map) {
        List<StockSkuSnapPageDto> result = dao.productStockAge(map);
        return result;
    }

    /**
     * 产品库龄导出
     *
     * @param map
     * @param response
     */
    @Override
    public void productStockAgeExport(Map<String, Object> map, HttpServletResponse response) {
        map.put("type", 1);
        List<StockSkuSnapPageDto> list = baseMapper.productStockAge(map);
        List<ProductStockAgeExportVo> exportVos = BeanConvert.INSTANCE.productStockAgeExportVo(list);
        String fileName = "产品" + "标准库龄" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, ProductStockAgeExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 箱库龄导出
     *
     * @param map
     * @param response
     */
    @Override
    public void boxStockAgeExport(Map<String, Object> map, HttpServletResponse response) {
        map.put("type", 2);
        List<StockSkuSnapPageDto> list = baseMapper.productStockAge(map);
        List<BoxStockAgeExportVo> exportVos = BeanConvert.INSTANCE.boxStockAgeExportVo(list);
        String fileName = "箱标准库龄" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, BoxStockAgeExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * Oms- 标准库龄 type = 1 产品 type = 2 箱
     */
    @Override
    public PageResult productStockAge(Map<String, Object> map) {
        IPage<StockSkuSnapPageDto> page = pageInit(map);
        page = dao.productStockAge(page, map);
        if (Objects.equals(map.get("type"), 2)) {
            for (StockSkuSnapPageDto record : page.getRecords()) {
                record.setSkuList(orderInputDao.getBoxProductListByBoxTypeNumber(record.getSku(), record.getCompanyId()));
            }
        }
        PageResult result = new PageResult(page.getRecords(), (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }


    /**
     * oms -分段库零 type = 1 产品 type = 2 箱
     *
     * @param map
     * @return
     */
    @Override
    public PageResult productSegAge(Map<String, Object> map) {
        IPage<StockSkuSegmentPageDto> page = pageInitSeg(map);
        page = dao.productSegAge(page, map);
        if (Objects.equals(map.get("type"), 2)) {
            for (StockSkuSegmentPageDto record : page.getRecords()) {
                record.setSkuList(orderInputDao.getBoxProductListByBoxTypeNumber(record.getSku(), record.getCompanyId()));
            }
        }
        PageResult result = new PageResult(page.getRecords(), (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }


    /**
     * 产品分段库龄导出
     *
     * @param map
     */
    @Override
    public void productSegAgeExport(Map<String, Object> map, HttpServletResponse response) {
        map.put("type", 1);
        List<StockSkuSegmentPageDto> list = dao.productSegAge(map);
        List<ProductStockSegAgeExportVo> exportVos = BeanConvert.INSTANCE.productStockSegAgeExportVo(list);
        String fileName = "产品分段库龄" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, ProductStockSegAgeExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 箱分段库龄导出
     *
     * @param map
     */
    @Override
    public void boxSegAgeExport(Map<String, Object> map, HttpServletResponse response) {
        map.put("type", 2);
        List<StockSkuSegmentPageDto> list = dao.productSegAge(map);
        List<BoxStockSegAgeExportVo> exportVos = BeanConvert.INSTANCE.boxStockSegAgeExportVo(list);
        String fileName = "箱分段库龄" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, BoxStockSegAgeExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    private IPage<StockSkuSegmentPageDto> pageInitSeg(Map<String, Object> map) {
        IPage<StockSkuSegmentPageDto> page = new Page<>();
        page.setSize(map.get("pageSize") != null ? Long.parseLong(map.get("pageSize").toString()) : 10);
        page.setCurrent(map.get("current") != null ? Long.parseLong(map.get("current").toString()) : 1);
        return page;
    }

    private IPage<StockSkuSnapPageDto> pageInit(Map<String, Object> map) {
        IPage<StockSkuSnapPageDto> page = new Page<>();
        page.setSize(map.get("pageSize") != null ? Long.parseLong(map.get("pageSize").toString()) : 10);
        page.setCurrent(map.get("current") != null ? Long.parseLong(map.get("current").toString()) : 1);
        return page;
    }

    @Override
    public void insertBatchMy(List<StockAgeEntity> list) {
        if (list == null || list.isEmpty()) return;
        dao.insertBatch(list);
    }

}
